<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理学科</title>

    <link href="bootstrap-4.6.1-dist/css/bootstrap.css"
          rel="stylesheet" type="text/css">
    <style>
        .dropdown{
            margin-bottom: -20px;
        }
        .dropdown:hover{
            margin: 0px;
        }
        .userPortrait {
            width: 40px;
            height: 40px;
            border: 1px solid red;
            border-radius: 50%;
            overflow: hidden;
            margin: 0 auto;
            /*margin-top: 100px;*/
        }
        .userPortrait img {
            display: block;
            width: 100%;
            height:100%;
            border: 1px solid green;
        }
        .welcome{
            width: 100%;
            height: 100px;
            float: left;
        }
        .subject{
            margin-top: 60px;
        }

    </style>
</head>
<body>
<!-- Modal -->
<div id="editModal" class="modal fade" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">编辑</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="editform">

                    <div class="form-group row">
                        <label class="col-form-label col-2 text-center">编号：</label>
                        <div class="col-8">
                            <input type="text" id="editsubjectNumber" name="subjectNumber" class="form-control">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-form-label col-2 text-center">学科：</label>
                        <div class="col-8">
                            <input type="text" id="editsubjectName" name="subjectName" class="form-control">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-form-label col-2 text-center">学分：</label>
                        <div class="col-8">
                            <input type="text" id="editsubjectCredit" name="subjectCredit" class="form-control">
                        </div>
                    </div>
                    <!-- 存放要修改数据的主键值，作为条件使用 -->
                    <input type="hidden" name="subjectId" id="editsubjectId">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick='doEdit()'>保存</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="delModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="delModalLabel">提示</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                您确定要删除【<span id="delsubjectName"></span>】吗?
                <form id="delForm">
                    <input type="hidden" name="ids" id="delsubjectId">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick='doDelete()'>确定</button>
            </div>
        </div>
    </div>
</div>
<!--眉头-->
<nav class="nav navbar-expand-lg navbar-dark bg-primary">
    <a class="navbar-brand col-1 text-center" href="index.html" >
        爱思教育
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse
    " data-target="#navbarText" aria-controls="navbarText" aria-expanded="false
    " aria-label="Toggle navigation">
        <span class="navbar-toggler-icon">111</span>
    </button>
    <div class="collapse navbar-collapse">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link"></a>
            </li>
            <li class="nav-item active">
                <a class="nav-link " href="managerHome.html">
                    点击此处返回管理员主页
                </a>
            </li>

        </ul>
    </div>
    <div class="userPortrait active dropdown">
        <a href="userInformation.html"><img src="img/defaultPortrait.jpg" alt=""></a>
    </div>
</nav>

<!--中间-->
<div class="row">
    <!--侧边栏-->
    <ul class="navbar-nav bg-light text-center col-md-2">
        <li class="nav-item">
            <a class="nav-link btn btn-light" href="#">消息列表</a>
        </li>
    </ul>
    <div  id="main" class="main col-10">
        <div id="top" class="main col-10 welcome">
            <font face="隶书" size="+4" color=black> 管理员你好，欢迎使用教务系统</font>
        </div>
        <div id="center" class="main">
            <form id="form">
                <table>
                    <tr>
                        <td>学科名：</td>
                        <td>
                            <input type="text" class="form-control" name="subjectName">
                        </td>
                        <td>学科编号：</td>
                        <td>
                            <input type="text" class="form-control" name="subjectNumber">
                        </td>
                        <td>学科学分：</td>
                        <td>
                            <input type="text" class="form-control" name="subjectCredit">
                        </td>
                        <td>
                            <button id="btnSearch" class="btn btn-primary  align-content-center text-center" type="button" >查询</button>
                        </td>
                        <td>
                            <button id="btnAdd" class="btn btn-primary  align-content-center text-center" type="button" >添加</button>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <div id="bottom" class="main col-10 subject">
            <form id="batchForm">
                <table class="table table-hover table-bordered">
                    <thead class="thead-light">
                    <tr>
                        <th scope="col">学科编号</th>
                        <th scope="col">学科名</th>
                        <th scope="col">学科学分</th>
                        <th scope="col">操作</th>
                    </tr>
                    </thead>

                    <tbody id="tbody">
                    <tr>
                        <td colspan="4" class="text-center">没有数据</td>
                    </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</div>
<!--页脚-->
<div class="footer bg-primary fixed-bottom text-center">
    <p class="text-white">版权所有© Copyright 1999-2021 爱思软件股份有限公司　　中国·青岛黄岛区嘉陵江路777号 西区公寓2号   版本V-0.0.1</p>
</div>



</body>

<script src="jQuery/jquery-3.6.0.min.js"></script>
<script src="bootstrap-4.6.1-dist/js/bootstrap.min.js"></script>
<script>
    jQuery(function () {
        jQuery("#btnSearch").click(function () {
            let form=jQuery("#form").serialize();
            jQuery.post("manager/Search",form,function (rst) {
                let trs='';

                for(let i=0;i<rst.length;i++)
                {
                    let u=rst[i];
                    trs+=`
                        <tr>
                            <td>${u.subjectNumber}</td>
                            <td>${u.subjectName}</td>
                            <td>${u.subjectCredit}</td>
                            <td>
                                <a href="#" onclick='showDeleteDialog(${JSON.stringify(u)})'>删除</a>
                                <a href="#" onclick='showEditDialog(${JSON.stringify(u)})'>编辑</a>
                            </td>
                        </tr>
                    `;
                }
                if(trs.length==0)
                {
                    trs=`
                         <tr>
                            <td colspan="4" class="text-center">无信息</td>
                        </tr>
                    `;
                }
                //把数据添加到tbody中
                jQuery("#tbody").html(trs);

            })
        });
    })
</script>

<script>
    jQuery(function () {
        jQuery("#btnAdd").click(function () {
            //获取页面中控件的数据
            //1.必须在<form></form>之间
            //2.必须有name属性
            let form=jQuery("#form").serialize();
            //向服务器发请求，并获取响应结果
            //rst表示服务器返回的数据
            jQuery.post("manager/Search_subjectNum",form,function (rst) {
                if(rst.length>0)
                {
                    alert("课程号重复");
                }
                else
                {
                    jQuery.post("manager/Add",form,function (rst) {
                        if(rst==1)
                        {
                            alert("添加成功");
                        }
                    });
                }
            });
        });
    })
</script>

<script>
    function showEditDialog(subject)
    {
        console.log(subject)
        jQuery("#editsubjectId").val(subject.subjectId);
        jQuery("#editsubjectNumber").val(subject.subjectNumber);
        jQuery("#editsubjectName").val(subject.subjectName);
        jQuery("#editsubjectCredit").val(subject.subjectCredit);
        jQuery("#editModal").modal('show')
    }

    function doEdit() {
        let form=jQuery("#editform").serialize();
        jQuery.post("manager/Update",form,function (rst) {
            if(rst>0)
            {
                jQuery("#editModal").modal("hide");
                alert("修改成功！");
                init();
            }else{
                alert("修改失败！");
            }
        })
    }
</script>
<script>
    function showDeleteDialog(subject) {
        console.log(subject);
        jQuery("#delsubjectName").html(subject.subjectName);
        jQuery("#delsubjectId").val(subject.subjectId);
        jQuery("#delModal").modal('show');
    }
    function doDelete() {
        let form=jQuery("#delForm").serialize();
        jQuery.post("manager/Delete",form,function (rst) {
            if(rst>0)
            {
                alert("删除成功！");
                init();
            }else{
                alert("删除失败！");
            }
        });
        jQuery("#delModal").modal('hide');
    }
</script>
</html>


